home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / COMMON.ZIP / CPUID.TXT < prev    next >
Encoding:
Text File  |  1995-08-30  |  5.6 KB  |  256 lines

  1. // C file ************************************************************
  2.  
  3. /*
  4.  
  5.   I compiled the C part with an old compiler that would generate old
  6.   8086 code so this would run on even an XT. Be careful, many new
  7.   compilers don't support that option, and will generate code that
  8.   will crash old computers.
  9.  
  10. */
  11. #include <stdio.h>
  12.  
  13.  
  14. /* ---------------------- main() ----------------------- January 13,1995 */
  15. void main(void)
  16. {
  17.    int i;
  18.    static char *cpu_str[]=
  19.       {
  20.       "i86", 
  21.       "i186",
  22.       "i286",
  23.       "i386",
  24.       "i486",
  25.       "i586",
  26.       "i686"
  27.       };
  28.  
  29.    i=x_processor();
  30.  
  31.    if ( i > 6 )
  32.       i=6;
  33.  
  34.    printf("Processor type: %s   CoPro : %s\n", cpu_str[i],
  35.          x_coprocessor() ? "Yes" : "No");
  36.  
  37.  
  38. }
  39.  
  40. // END C file ************************************************************
  41.  
  42.  
  43. ;-----------------------------------------------------------------------
  44. ; MODULE XDETECT
  45. ;
  46. ; Hardware detection module
  47. ;
  48. ; Compile with Tasm.
  49. ; C callable.
  50. ;
  51. ;
  52. ; ****** XLIB - Mode X graphics library                ****************
  53. ; ******                                               ****************
  54. ; ****** Written By Themie Gouthas                     ****************
  55. ;
  56. ;
  57. ; modified May, 1993 by Alec Russell - 
  58. ; to use TASM high level language extensions
  59. ;
  60. ; egg@dstos3.dsto.gov.au
  61. ; teg@bart.dsto.gov.au
  62. ;-----------------------------------------------------------------------
  63.  
  64. .model medium, c
  65.  
  66.      global x_processor            :proc
  67.      global x_coprocessor          :proc
  68.  
  69.  
  70. LOCALS
  71. .386
  72.  
  73.  
  74. CPUID   MACRO
  75.         db    0fh,     0A2h
  76. ENDM
  77.  
  78.     .code
  79.  
  80.  
  81. i86       equ 0
  82. i186      equ 1
  83. i286      equ 2
  84. i386      equ 3
  85. i486      equ 4
  86. i586      equ 5
  87.  
  88.  
  89.  
  90. ;-----------------------------------------------------------------------
  91. ; PC Processor detection routine
  92. ;
  93. ; C callable as:
  94. ;    unsigned int x_processor();
  95. ;
  96. ;
  97. x_processor PROC
  98. .8086
  99.     pushf                    ; Save flags
  100.  
  101.     xor  ax,ax         ; Clear AX
  102.     push ax                  ; Push it on the stack
  103.     popf                     ; Zero the flags
  104.     pushf                    ; Try to zero bits 12-15
  105.     pop  ax                  ; Recover flags
  106.         and  ax,0F000h           ; If bits 12-15 are 1 => i86 or i286
  107.         cmp  ax,0F000h
  108.         jnz  @@not_86_186
  109.         jmp  @@is_86_186
  110.  
  111. @@not_86_186:
  112.  
  113.         mov  ax,07000h           ; Try to set bits 12-14
  114.         push ax
  115.         popf
  116.         pushf
  117.         pop  ax
  118.         and  ax,07000h           ; If bits 12-14 are 0 => i286
  119.         jnz   is_not_286
  120.         jmp is_286
  121.  
  122. is_not_286:
  123.  
  124.  
  125.         ; its a 386 or higher
  126.  
  127.         ; check for 386 by attempting to toggle EFLAGS register
  128.         ; Alignment check bit which can't be changed on a 386
  129. .386
  130.         cli
  131.         pushfd
  132.         pushfd
  133.         pop   eax
  134.         mov  ebx, eax
  135.         xor   eax, 040000h      ; toggle bit 18
  136.         push  eax
  137.         popfd
  138.         pushfd
  139.         pop   eax
  140.         popfd
  141.         sti
  142.         and   eax, 040000h      ; clear all but bit 18
  143.         and   ebx, 040000h      ; same thing
  144.         cmp   eax, ebx
  145.         jne   @@moretest
  146.         mov   ax, i386
  147.         jmp short @@done
  148.  
  149.         ; is it a 486 or 586 or higher
  150.  
  151. @@moretest:
  152.  
  153.         ; check for a 486 by trying to toggle the EFLAGS ID bit
  154.         ; this isn't a foolproof check
  155.  
  156.         cli
  157.         pushfd
  158.         pushfd
  159.         pop   eax
  160.         mov   ebx, eax
  161.         xor   eax, 0200000h      ; toggle bit 21
  162.         push  eax
  163.         popfd
  164.         pushfd
  165.         pop   eax
  166.         popfd
  167.         sti
  168.         and   eax, 0200000h      ; clear all but bit 21
  169.         and   ebx, 0200000h      ; same thing
  170.         cmp   eax, ebx
  171.         jne   @@moretest2
  172.         mov   ax, i486
  173.         jmp short @@done
  174.  
  175. @@moretest2:
  176.  
  177.         ; OK it was probably a 486, but lets double check
  178.  
  179.         mov   eax, 1
  180.         CPUID
  181.         and   eax, 0f00h
  182.         shr   eax, 8
  183.  
  184.         mov   ebx, eax
  185.         mov   ax, i586
  186.         cmp   ebx, 5
  187.         je    @@done    ; it was a pentium
  188.  
  189.         ; it wasn't a 586 so just report the ID
  190.  
  191.         mov   eax, ebx
  192.         and   eax, 0ffffh
  193.  
  194.         jmp  short @@done
  195.  
  196. .8086
  197.  
  198. is_286:
  199.         mov  ax,i286             ; We have a 286
  200.         jmp  short @@done
  201.  
  202. @@is_86_186:                       ; Determine whether i86 or i186
  203.         push cx                  ; save CX
  204.         mov  ax,0FFFFh           ; Set all AX bits
  205.         mov  cl,33               ; Will shift once on 80186
  206.         shl  ax,cl               ; or 33 x on 8086
  207.         pop  cx
  208.         jnz  is_186              ; 0 => 8086/8088
  209. is_86:
  210.         mov  ax,i86
  211.         jmp  short @@done
  212. is_186:
  213.         mov  ax,i186
  214. @@done:
  215.     popf
  216.  
  217.     ret
  218.  
  219. x_processor endp
  220.  
  221. .386
  222.  
  223.  .8086
  224. ;-----------------------------------------------------------------------
  225. ; PC Numeric coprocessor detection routine
  226. ;
  227. ; C callable as:
  228. ;    unsigned int x_coprocessor();
  229. ;
  230. ;  Based on an article in PC Tech Journal, Aug 87 by Ted Forgeron
  231. ;
  232. ;  Returns 1 if coprocessor found, zero otherwise
  233.  
  234. x_coprocessor PROC
  235.  
  236.         LOCAL     control:word
  237.  
  238.     fninit                          ; try to initialize the copro.
  239.     mov    [control],0              ; clear control word variable
  240.         fnstcw control                  ; put control word in memory
  241.     mov    ax,[control]             ;
  242.     cmp    ah,03h                   ; do we have a coprocessor ?
  243.     je     @@HaveCopro              ; jump if yes!
  244.     xor    ax,ax                    ; return 0 since nothing found
  245.     jmp    short @@Done
  246. @@HaveCopro:
  247.     mov    ax,1
  248. @@Done:
  249.     ret
  250.  
  251. x_coprocessor   endp
  252.  
  253.  
  254. end
  255.  
  256.